package com.hs.demo.mina;

import com.mysql.cj.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
@Slf4j
public class TimeServerHandler extends IoHandlerAdapter {
    private int count = 0;
    public static String cach_temp = "";//丢包临时缓存

    private SenMessageService senMessageServiceX;

    public TimeServerHandler(SenMessageService senMessageService) {
        this.senMessageServiceX = senMessageService;
    }

    @Override
    public void messageReceived(IoSession session, Object message) {
        byte[] by = new byte[((IoBuffer)message).limit()];
        ((IoBuffer)message).get(by);
        StringBuffer mesbu = new StringBuffer();
        for (int i = 0; i < by.length; i++) {
            mesbu.append((char) by[i]);
        }
        String mes = mesbu.toString();
        log.info("收到二维码：" + mes);
        if(mes.substring(0,2).equals("Al") && mes.contains("OtherID")){
            log.info("验证码完整，开始解析：");
            senMessageServiceX.insert(mes);
        }else {
            if(mes.substring(0,2).equals("Al") && !mes.contains("OtherID")){
                log.info("前包");
                cach_temp = mes;
                log.info("cach:" + cach_temp);
            }
            if(!mes.substring(0,2).equals("Al") && mes.contains("OtherID")){
                log.info("后包");
                log.info("cach:" + cach_temp);
                if(!StringUtils.isNullOrEmpty(cach_temp)){
                    log.info("合并");
                    cach_temp = cach_temp + mes;
                    log.info("解析合并:" + cach_temp);
                    senMessageServiceX.insert(cach_temp);
                    cach_temp = "";
                }
            }
        }
        /**
         * login,gp3324Al,0.023352967996031,C,3.5800689460435,Ce,0.0321282110937702,
         * Cr,0.514473199400084,Cu,0.284683394715809,Fe,92.2106257825719,Mg,0.0391158608090413,Mn,0.458960243310403,Mo,0.294405469357827,Ni,0.632517642447715,P,0.0453985894218692,S,0.0145857979867725,Sb,0.028331786229877,
         * Si,1.58313698569047,Sn,0.0106465667284521,Ti,0.0371219156022524,V,0.21044664059425,
         */
        /**
         * Al,0.0230271148861481,C,3.548930911013,Ce,0.0287192999483422,Cr,0.518170454330976,Cu,0.288027620392837,Fe,92.2412924805239,Mg,0.0372328330673674,Mn,0.460368639549601,Mo,0.291630344289515,Ni,0.634269330724549,P,0.0452830118243413,S,0.0142214500994483,Sb,0.030547907699437,Si,1.5787624934271,
         * Sn,0.0114128343305496,Ti,0.0375299962731585,V,0.210573277619769,,3-3-1-B,SampleID,66LC11001,Worker,,OtherID,
         */
//        String substring = mes;

    }

    public static void main(String[] args) {
        String s = "login,gp3324Al,0.023352967996031,C,3.5800689460435,Ce,0.0321282110937702,Cr,0.514473199400084,Cu,0.284683394715809,Fe,92.2106257825719,Mg,0.0391158608090413,Mn,0.458960243310403,Mo,0.294405469357827,Ni,0.632517642447715,P,0.0453985894218692,S,0.0145857979867725,Sb,0.028331786229877,Si,1.58313698569047,Sn,0.0106465667284521,Ti,0.0371219156022524,V,0.21044664059425,";
        String substring = s.substring(12);
        System.out.println(substring);
        String[] split = substring.split(",");
        System.out.println(Arrays.asList(split));
        for (int i = 0;i< split.length ;i++){
            if(split[i].equals("Al")){
                System.out.println(split[i + 1]);
            }
            if(split[i].equals("C")){
                System.out.println(split[i + 1]);
            }
        }



    }

}

